// pages/hypermarket/hypermarket.js
import WxmpRsa from 'wxmp-rsa'
// 用户中心的token去换取H5的token 
// 实例化rsa
const rsa = new WxmpRsa()
// 定义待加密的字符串
// const str = 'token'
if (getApp().globalData.key == true) {
  // 正式密匙
  var publicKey = `
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDheE+/lZ49TIsMnMxAFUTilkUX
  WCmlVUDUTx821sMaa4EsVM/aimoGetHZiV+9SF0+L0VCSYtol0QRCrlZT6FLA179
  p5QbrMPexeL8lyKfcIv4oLt8x/N0dCXFi5KlBhujDKO/fTKNmWX65IK3fitxpmrO
  1/IXCt7RgaD/jn2ZcQIDAQAB`
} else {
  // 测试密匙
  var publicKey = `
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjN9RgZJkhylel9AZm6PQAS4hlGsvJXxthDoKfBtkhYEAQjDXk/0NP+mmqHefYsXrooyHl9c1+e+mfKGqIXxq84lgWYIyjAX3kyAD3cU1wsoNTtg0TpjqPXP28luHmTY+yChrXuOaTZmWx7gjbAQqYAEoTLdohne5F/tX2nbtU0QIDAQAB`
}



// 设置公钥
rsa.setPublicKey(publicKey)
// 加密
// const cryptStr = rsa.encryptLong(str)
// console.log('加密后的结果：', cryptStr)

// 定义私钥
// const privateKey = `
// -----BEGIN RSA PRIVATE KEY-----
// MIICWgIBAAKBgFnWSUwsmGawhMJ30z6y5li2jcf1m7rPMZcwZOS3To8bk3OBaMGh
// VEc1F8GtJBbc1rn/HCLNL9zrCy21EefJON8tRFcYHnpseZSzh+349lIhS+MFw9x4
// JUddwSPDyxwha929cKzMuVoftu3CJ+kqDBVvxLk7iDBzUMqW3Kgehk2TAgMBAAEC
// gYBRChPeyk/EOrHX912xLpLKLguh+LY9g1B50ScChzUvtTGDPZaxLQYoogVHKhfn
// I9nzuOS5pBzsDX9tAO0hCQzqfHgqRjn+vEgm1Ui+f0E3BVRnhobcJKZpZqlvCBR5
// Gu2+zlrY4SeGq3AuQSr/A5FiB5k0RgsvNycDTjqyg7TXGQJBAJoZ8Yr0zakxT1I8
// lVqsFbeNPtt8FNG2UgIlIs9RL7aXhw+Y3sWtk/kbaOXafSofu0NcQYx4Km3M3kiP
// lcNfTJ8CQQCVPcaRpu+mprRgHS6s76Z668NaFsjX04CUUa0kCrey+Nf/SJJ3BkRH
// M7GllZWuI/RSXs/F5N38p5bfkn7QZqaNAkBy3dHJZW8DpgjdYOFnhAxwFK39BwGx
// zHhWtv26kWbCcTKwsp+jtB4vunm3k+RmiN6aeGM35L6jt+kdJ0JYLmo7AkBJpRZb
// wZj5D8Jqu3vQ8uGgPr9DsYKinkgQ6M0bv/4uXwWXf+Rmv7zpteSv5UTbjfp+uzKk
// YO/6QWj+InhZto3xAkAOA0i702dLHm5elLWvht7UEYIDEW1+rYGdbthmJBvT9sZh
// VKL954Y9hDzBWepjYsBiJnmIkgeladPnU5025/G/
// -----END RSA PRIVATE KEY-----
// `
// 设置私钥
// rsa.setPrivateKey(privateKey)
// 解密
// const originalStr = rsa.decryptLong(cryptStr)
// console.log('解密后的原始数据：', originalStr)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accessToken: '',
    refreshToken: '',
    webSrc: '',
    hide: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  payment() {
    wx.navigateTo({
      url: '../../packageA/payment/payment',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.hide) {
      let that = this
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          console.log(res)
          const latitude = res.latitude
          const longitude = res.longitude
          wx.login({
            success(res) {
              let accessToken = rsa.encryptLong(wx.getStorageSync('accessToken'))
              let refreshToken = rsa.encryptLong(wx.getStorageSync('refreshToken'))
              let webSrc = "accessToken=" + encodeURIComponent(accessToken) + "&refreshToken=" + encodeURIComponent(refreshToken) + "&authCode=" + res.code + '&latitude=' + latitude + "&longitude=" + longitude;
              that.setData({
                accessToken: wx.getStorageSync('accessToken'),
                refreshToken: wx.getStorageSync('refreshToken'),
                webSrc: getApp().globalData.webSrc + '/geely-car?' + webSrc
              })
            }
          })
        },
        fail(ree) {
          wx.login({
            success(res) {
              let accessToken = rsa.encryptLong(wx.getStorageSync('accessToken'))
              let refreshToken = rsa.encryptLong(wx.getStorageSync('refreshToken'))
              let webSrc = "accessToken=" + encodeURIComponent(accessToken) + "&refreshToken=" + encodeURIComponent(refreshToken) + "&authCode=" + res.code;
              that.setData({
                accessToken: wx.getStorageSync('accessToken'),
                refreshToken: wx.getStorageSync('refreshToken'),
                webSrc: getApp().globalData.webSrc + '/geely-car?' + webSrc
              })
            }
          })
        }
      })
    }
  },
  onTabItemTap(item) {
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
    this.setData({
      hide: true
    })
    if (item.pagePath == 'pages/hypermarket/hypermarket' && this.data.hide) {
      let that = this
      let accessToken = wx.getStorageSync('accessToken')
      let refreshToken = wx.getStorageSync('refreshToken')
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          console.log(res)
          const latitude = res.latitude
          const longitude = res.longitude
          wx.login({
            success(res) {
              let accessToken = rsa.encryptLong(wx.getStorageSync('accessToken'))
              let refreshToken = rsa.encryptLong(wx.getStorageSync('refreshToken'))
              let webSrc = "accessToken=" + encodeURIComponent(accessToken) + "&refreshToken=" + encodeURIComponent(refreshToken) + "&authCode=" + res.code + '&latitude=' + latitude + "&longitude=" + longitude;
              that.setData({
                accessToken: wx.getStorageSync('accessToken'),
                refreshToken: wx.getStorageSync('refreshToken'),
                webSrc: getApp().globalData.webSrc + '/geely-car?' + webSrc
              })
            }
          })
        },
        fail(ree) {
          wx.login({
            success(res) {
              let accessToken = rsa.encryptLong(wx.getStorageSync('accessToken'))
              let refreshToken = rsa.encryptLong(wx.getStorageSync('refreshToken'))
              let webSrc = "accessToken=" + encodeURIComponent(accessToken) + "&refreshToken=" + encodeURIComponent(refreshToken) + "&authCode=" + res.code;
              that.setData({
                accessToken: wx.getStorageSync('accessToken'),
                refreshToken: wx.getStorageSync('refreshToken'),
                webSrc: getApp().globalData.webSrc + '/geely-car?' + webSrc
              })
            }
          })
        }
      })
    } else {

    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('onHide')
    this.setData({
      hide: false
    })

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('onUnload')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})